//
//  AnnularProgressView.swift
//  NCMProduction
//
//  Created by 张文晏 on 2018/11/13.
//  Copyright © 2018 张文晏. All rights reserved.
//

import UIKit
import SnapKit

public class AnnularProgressView: UIView {
    /// 进度槽
    let trackLayer = CAShapeLayer()
    /// 进度条
    let progressLayer = CAShapeLayer()
    /// 进度条路径（整个圆圈）
    let path = UIBezierPath()
    /// 进度文字
    let progressLabel = UILabel.init(frame: CGRect.zero)
    
    //当前进度
    @IBInspectable public var progress: Int = 0 {
        didSet {
            if progress > 100 {
                progress = 100
            }else if progress < 0 {
                progress = 0
            }
        }
    }
    /// 进度条颜色
    public var progressColor = UIColor.init(hex: 0x006fff){
        didSet{
            progressLabel.textColor = progressColor
        }
    }
    /// 进度槽颜色
    public var trackColor = UIColor(red: 245/255.0, green: 245/255.0, blue: 245/255.0,
                                    alpha: 1)
    /// 进度条宽度
    public var lineWidth: CGFloat = 6
    /// 是否显示进度文字
    public var showProgressText = true{
        didSet{
            progressLabel.isHidden = !showProgressText
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    func setupUI(){
        progressLabel.font = UIFont.systemFont(ofSize: 8)
        progressLabel.textColor = UIColor.init(hex: 0x006fff)
        addSubview(progressLabel)
        progressLabel.snp.makeConstraints { (maker) in
            maker.center.equalToSuperview()
        }
    }
    
    override public func draw(_ rect: CGRect) {
        path.addArc(withCenter: CGPoint(x: bounds.midX, y: bounds.midY),
                                                radius: bounds.size.width/2 - lineWidth,
                                                                                                startAngle: angleToRadian(-90), endAngle: angleToRadian(270), clockwise: true)
        
        trackLayer.frame = bounds
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.strokeColor = trackColor.cgColor
        trackLayer.lineWidth = lineWidth
        trackLayer.path = path.cgPath
        layer.addSublayer(trackLayer)
        
        progressLayer.frame = bounds
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = progressColor.cgColor
        progressLayer.lineWidth = lineWidth
        progressLayer.path = path.cgPath
        progressLayer.strokeStart = 0
        progressLayer.strokeEnd = CGFloat(progress)/100.0
        layer.addSublayer(progressLayer)
    }
    
    public  func setProgress(_ pro: Int,animated anim: Bool) {
        setProgress(pro, animated: anim, withDuration: 0.55)
    }
    
    public func setProgress(_ pro: Int,animated anim: Bool, withDuration duration: Double) {
        progress = pro
        progressLabel.text = "\(pro)%"
        CATransaction.begin()
        CATransaction.setDisableActions(!anim)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name:
            CAMediaTimingFunctionName.easeInEaseOut))
        CATransaction.setAnimationDuration(duration)
        progressLayer.strokeEnd = CGFloat(progress)/100.0
        CATransaction.commit()
    }
    
    fileprivate func angleToRadian(_ angle: Double)->CGFloat {
        return CGFloat(angle/Double(180.0) * .pi)
    }
}
